home *** CD-ROM | disk | FTP | other *** search
- /* ----------------------------------------------------------------
- * Clock 1.0, Copyright (c) 1995 Nils Hedstr├╢m, All Rights Reserved.
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for NON-COMMERCIAL purposes and without fee is hereby
- * granted provided that this copyright notice and appropiate documention
- * appears in all copies.
- *
- * NILS HEDSTRÖM MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
- * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
- * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. NILS HEDSTRÖM SHALL NOT BE LIABLE
- * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
- * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- *
- * For documentation look at http://www-und.ida.liu.se/~d94nilhe/java/applets.html */
-
- import browser.Applet;
- import awt.Graphics;
- import awt.WServer;
- import awt.Color;
- import awt.Image;
- import java.util.Date;
- import java.lang.Math;
- import net.www.html.URL;
-
- class Clock extends Applet implements Runnable
- {
- int width,height,num_lines,hour,minute,second,sleep,hour_width,minute_width,second_width;
- int hour_type,minute_type,second_type;
- double pi=3.1415926535,hour_len,minute_len,second_len;
- Color hour_col,minute_col,second_col,border_col,background_col;
- URL homepage=new URL("http://www-und.ida.liu.se/~d94nilhe/java/applets.html");
- Thread animate=null;
- Image im;
- Graphics offscreen;
- public void init()
- {
- width=getIntAttribute("width",0,200,1000);
- height=getIntAttribute("height",0,200,1000);
- num_lines=getIntAttribute("num_lines",3,12,1000);
- sleep=getIntAttribute("sleep",5,1000,20000);
- hour_len=getIntAttribute("hour_len",0,60,100)/200.;
- minute_len=getIntAttribute("minute_len",0,75,100)/200.;
- second_len=getIntAttribute("second_len",0,90,100)/200.;
- hour_width=getIntAttribute("hour_width",0,7,100);
- minute_width=getIntAttribute("minute_width",0,5,100);
- second_width=getIntAttribute("second_width",0,3,100);
- hour_type=getIntAttribute("hour_type",0,3,3);
- minute_type=getIntAttribute("minute_type",0,3,3);
- second_type=getIntAttribute("second_type",0,3,3);
- hour_col=StrHexToColor(getStringAttribute("hour_col","ff0000"));
- minute_col=StrHexToColor(getStringAttribute("minute_col","00ff00"));
- second_col=StrHexToColor(getStringAttribute("second_col","0000ff"));
- border_col=StrHexToColor(getStringAttribute("border_col","000000"));
- background_col=StrHexToColor(getStringAttribute("background_col","c0c0c0"));
-
- resize(width,height);
- try
- {
- im = createImage(width, height);
- offscreen = new Graphics(im);
- }
- catch (Exception e)
- {
- // double-buffering not available
- offscreen = null;
- }
- }
- public void paintApplet(Graphics g)
- {
- int x1,x2,x3,y1,y2,y3;
- x1=(int)(second_len*width*Math.cos(2.*pi*second/60.)+width/2.);
- y1=(int)(second_len*height*Math.sin(2.*pi*second/60.)+height/2.);
- x2=(int)(minute_len*width*Math.cos(2.*pi*minute/60.)+width/2.);
- y2=(int)(minute_len*height*Math.sin(2.*pi*minute/60.)+height/2.);
- x3=(int)(hour_len*width*Math.cos(2.*pi*(hour/12.+(minute+15)/720.))+width/2.);
- y3=(int)(hour_len*height*Math.sin(2.*pi*(hour/12.+(minute+15)/720.))+height/2.);
- g.SetColor(background_col);
- g.fillRect(0, 0, width, height);
- circle(g,width/2,height/2,width/2,height/2,num_lines,border_col);
- g.SetColor(hour_col);
- drawArm(g,(int)(width/2.),(int)(height/2.),x3,y3,hour_width,hour_type);
- g.SetColor(minute_col);
- drawArm(g,(int)(width/2.),(int)(height/2.),x2,y2,minute_width,minute_type);
- g.SetColor(second_col);
- drawArm(g,(int)(width/2.),(int)(height/2.),x1,y1,second_width,second_type);
- }
-
-
- void DrawPoly(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4,
- Graphics g)
- {
-
- int x, mny, mxy, mnx, mxx, yc;
- int mul1, div1, mul2, div2, mul3, div3, mul4, div4;
-
-
- // find the maximum y (mny) and minimum y (mny)
-
- mxy=Math.max(Math.max(y1,y2),Math.max(y3,y4));
- mny=Math.min(Math.min(y1,y2),Math.min(y3,y4));
-
-
- // constants needed for intersection calculations
- mul1 = x1-x4; div1 = y1-y4;
- mul2 = x2-x1; div2 = y2-y1;
- mul3 = x3-x2; div3 = y3-y2;
- mul4 = x4-x3; div4 = y4-y3;
-
- for (yc=mny; yc<mxy; yc++) {
- mnx = width;
- mxx = -1;
-
- if ((y4 >= yc) || (y1 >= yc))
- if ((y4 <= yc) || (y1 <= yc))
- if (y4 != y1) {
- x = ((yc-y4) * mul1 / div1) + x4;
- if (x<mnx) mnx = x;
- if (x>mxx) mxx = x;
- }
-
- if ((y1 >= yc) || (y2 >= yc))
- if ((y1 <= yc) || (y2 <= yc))
- if (y1 != y2) {
- x = ((yc-y1) * mul2 / div2) + x1;
- if (x<mnx) mnx = x;
- if (x>mxx) mxx = x;
- }
-
- if ((y2 >= yc) || (y3 >= yc))
- if ((y2 <= yc) || (y3 <= yc))
- if (y2 != y3) {
- x = ((yc-y2) * mul3 / div3) + x2;
- if (x<mnx) mnx = x;
- if (x>mxx) mxx = x;
- }
-
- if ((y3 >= yc) || (y4 >= yc))
- if ((y3 <= yc) || (y4 <= yc))
- if (y3 != y4) {
- x = ((yc-y3) * mul4 / div4) + x3;
- if (x<mnx) mnx = x;
- if (x>mxx) mxx = x;
- }
-
-
-
- if (mnx<=mxx)
- // draw the horizontal line
- g.drawLine(mnx,yc,mxx,yc);
-
- }
- }
- public void drawArm(Graphics g, int x1,int y1, int x2,int y2, int arm_width, int arm_type)
- {
- int dx,dy;
- double len,add,n;
- len=Math.sqrt(Math.pow(x2-x1,2.)+Math.pow(y2-y1,2.));
- dx=(int)(arm_width*(y2-y1)/len);
- dy=(int)(arm_width*(x1-x2)/len);
- if(arm_width==1) g.drawLine(x1,y1,x2,y2);
- else
- {
- switch (arm_type)
- {
- case 0:
- break;
- case 1:
- DrawPoly(x1-dx,y1-dy,x1+dx,y1+dy,x2,y2,x2,y2,g);
- break;
- case 2:
- DrawPoly(x1-dx,y1-dy,x1+dx,y1+dy,x2+dx,y2+dy,x2-dx,y2-dy,g);
- break;
- case 3:
- DrawPoly(x1,y1,(x1+x2)/2+dx,(y1+y2)/2+dy,x2,y2,(x1+x2)/2-dx,(y1+y2)/2-dy,g);
- }
- }
- }
- public void paint(Graphics g)
- {
- if (offscreen != null)
- {
- // double-buffering available
- paintApplet(offscreen);
- g.drawImage(im, 0, 0);
- } else
- {
- // no double-buffering
- paintApplet(g);
- }
- }
- public void run() {
- while (true)
- {
- Date today= new Date();
- hour=today.getHours()%12-3;
- minute=today.getMinutes()-15;
- second=today.getSeconds()-15;
- repaint();
- Thread.sleep(sleep);
- }
- }
- public void mouseEnter()
- {
- showStatus("Who made this clock?");
- }
- public void mouseExit()
- {
- showStatus("");
- }
-
- public void mouseDown(int x,int y)
- {
- /*if(x>=0 && x<width && y>=0 && y<height)*/
- showDocument(homepage);
- }
- public void circle(Graphics g,int x, int y, int xr,int yr, int num,Color col)
- {
- double add,count;
- add=2.*pi/num;
- g.SetColor(col);
- for(count=0;count<=2.*pi;count+=add)
- {
- g.drawLine(x+(int)(xr*Math.sin(count)),
- y+(int)(yr*Math.cos(count)),
- x+(int)(xr*Math.sin(count+add)),
- y+(int)(yr*Math.cos(count+add)));
- }
- }
- public void start() {
- if (animate == null) {
- animate = new Thread(this);
- animate.start();
- }
- }
-
- public void stop() {
- if (animate != null) {
- animate.stop();
- animate=null;
- }
- }
- public void update(Graphics g)
- {
- paint(g);
- }
-
- public int charHexToInt(char in)
- {
- int dec;
- if (in>='0' && in<='9') return (int)(in-'0');
- if (in>='A' && in<='F') return (int)(in-'A'+10);
- if (in>='a' && in<='f') return (int)(in-'a'+10);
- return -1;
- }
- public Color StrHexToColor(String in)
- {
- Color col;
- int r,g,b;
- r=StrHexToInt(in.substring(0,2));
- g=StrHexToInt(in.substring(2,4));
- b=StrHexToInt(in.substring(4,6));
- if(r>=0 && g>=0 && b>=0) return new Color(r,g,b);
- else
- return Color.red;
- }
-
- public int StrHexToInt(String in)
- {
- int lown, highn;
- highn = charHexToInt(in.charAt(0)) * 16;
- lown = charHexToInt(in.charAt(1));
- if ((highn >= 0) && (lown >= 0))
- return (highn+lown);
- else
- return -1;
- }
-
-
-
-
- public int getIntAttribute (String att, int min, int def, int max)
- {
- try {
- int ret = Integer.parseInt(getAttribute(att));
- if (ret < min)
- return min;
- if (ret > max)
- return max;
- return ret;
- } catch(Exception e) {
- return def;
- }
-
- }
- public String getStringAttribute (String att, String def)
- {
- String ret;
-
- try {
- ret = getAttribute(att);
- if (ret.length() < 1)
- return def;
- return ret;
- } catch(Exception e) {
- return def;
- }
-
- }
- }
-
-
-